<template>
	<view class="content">
		<view v-for="(msg,index) in history" :key="index">
			{{msg}}
		</view>
		<input type="text" v-model="title" placeholder="请输入"></input>
		<button @click="submit()">发送</button>
	</view>
</template>

<script setup>
	const title = ref('你好')
	const history = ref([])
	import { useUserStore } from '@/stores/user.js'
	const userStore = useUserStore()
	// 通过按钮把消息传送给websocket
	function submit(){
		uni.sendSocketMessage({
		  data: title.value
		});
	}
	onLoad(() => {
		userStore.getInfo()
		// 建立websocket链接
		uni.connectSocket({
			url:'wss://echo.websocket.org',
			success: data => {
				console.log(data)
			}
		})
		// 获取webscoet数据
		uni.onSocketMessage(function (res) {
		  console.log('收到服务器内容：' + res.data);
		  history.value.push(res.data)
		});
	})
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
